package com.classactionpl.jaas;

import java.security.Principal;
import java.security.acl.Group;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class UserGroup extends UserPrincipal implements Group {
	Set<Principal> members;

	public UserGroup(String groupname) {
		super(groupname);
	}

	public boolean addMember(Principal user) {
		initMembers();
		return members.add(user);
	}

	void initMembers() {
		if (members == null) {
			members = new HashSet<Principal>();
		}
	}

	public boolean isMember(Principal member) {
		initMembers();
		return members.contains(member);
	}

	public Enumeration<? extends Principal> members() {
		initMembers();

		Enumeration<? extends Principal> enumeration = new Enumeration<? extends Principal>() {
			Iterator<? extends Principal> iter = members.iterator();

			public boolean hasMoreElements() {
				return iter.hasNext();
			}

			public Principal nextElement() {
				return iter.next();
			}
		};

		return enumeration;
	}

	public boolean removeMember(Principal user) {
		initMembers();
		return members.remove(user);
	}

}
